package de.g18.llmaa.logic.xmlRead;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import org.jdom.DataConversionException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import de.g18.llmaa.logic.vos.VOKey;
import de.g18.llmaa.logic.vos.VOKey_Type;
import de.g18.llmaa.logic.vos.VOProduct_Key;

public class XML_LizenzRead {

private	HashMap<String, VOKey_Type> list_KeyTypes = new HashMap<String, VOKey_Type>();
private ArrayList<VOProduct_Key> list_ProductKey = new ArrayList<VOProduct_Key>();

public XML_LizenzRead() {
		readLizenzXMLFile(new File("keys.xml"));
		
	}

	public XML_LizenzRead(File selectedFile) {
	readLizenzXMLFile(selectedFile);
}

	private void readLizenzXMLFile(File fileName) {
		Document doc;
		try {
			doc = new SAXBuilder().build(fileName);

			Element rootElement = doc.getRootElement();

			Iterator it = rootElement.getChildren().iterator();
			while (it.hasNext()) {
				Element productKey = (Element) it.next();
				parseProductKey(productKey);
			}

		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void parseProductKey(Element productKey) {
		VOProduct_Key correntProductKey = new VOProduct_Key();

		correntProductKey.setProduct_Name(productKey.getAttribute("Name")
				.getValue());

		Iterator it = productKey.getChildren().iterator();
		while (it.hasNext()) {
			Element correntKey = (Element) it.next();
			parseKey(correntKey, correntProductKey);
		}

		list_ProductKey.add(correntProductKey);
	}

	private void parseKey(Element productKey, VOProduct_Key correntProduct) {

		VOKey correntKey = new VOKey();

		try {
			correntKey.setKey_Id(productKey.getAttribute("ID").getIntValue());
			
			String tempClimedDate = productKey.getAttribute("ClaimedDate").getValue();
			
			correntKey.setKey_ClaimedDate(parseStringToDate(tempClimedDate));
			correntKey.setKey_Product(correntProduct);

			correntProduct.getList_Key().add(correntKey);
			correntKey.setKey_Serial(productKey.getValue());

			setKeyType(correntKey, productKey);
			

		} catch (DataConversionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void setKeyType(VOKey correntKey, Element productKey) {

		String typeText = productKey.getAttribute("Type").getValue();
		VOKey_Type type;


		type = list_KeyTypes.get(typeText);

		if (type == null) {
			type = new VOKey_Type();
			type.setType_Name(typeText);
			list_KeyTypes.put(typeText, type);

		}
//		type.getList_Key().add(correntKey);
		correntKey.setKey_Type(type);

	}

	
	private Date parseStringToDate(String str_date)
	{
		Date date = null;
 
		try {
 
 
			DateFormat formatter;
 
			formatter = new SimpleDateFormat("yyyy/MM/dd");
			date = (Date) formatter.parse(str_date);

 
			} catch (ParseException e) {
				System.out.println("Exception :" + e);
			}
 
		return date;
	}

	public HashMap<String, VOKey_Type> getList_KeyTypes() {
		return list_KeyTypes;
	}

	public ArrayList<VOProduct_Key> getList_ProductKey() {
		return list_ProductKey;
	}
	
	
	
	
}
